<?php
/**
 * Description:
 *
 * @author      Eden
 * @datetime    2017/3/26 23:53
 * @copyright   WuHan HuQi Technology Co.,Ltd.
 * @url         http://cnhuqi.com
 */
class Ticket_Model extends ADM_Model
{
    /**
     * 初始化
     */
    public function __construct()
    {
        parent::__construct();
    }

    /**
     * 添加优惠券
     * @param $params
     * @return mixed
     */
    public function add($params)
    {
        $db = clone($this->db);
        return $db->insert('promoter_ticket', $this->formatParams($params));
    }

    /**
     * 获取列表
     * @param int $offset
     * @param int $limit
     * @return array
     */
    public function getLists($offset = 0, $limit = 20)
    {
        $db = clone($this->db);
        $where = [
            'status >=' => 0
        ];

        $total = $db->where($where)->count_all_results('promoter_ticket');

        if ($total <= 0) {
            return [
                'total' => 0,
                'data' => []
            ];
        }

        $db->limit($limit, $offset);
        $db->order_by("id desc");
        $query = $db->get_where('promoter_ticket', $where);

        $res = $query->result_array();

        foreach ($res as &$row) {
            $row['time_start'] = date('Y-m-d', strtotime($row['time_start']));
            $row['time_end'] = date('Y-m-d', strtotime($row['time_end']));
            $row['amount'] = money_format('%i', $row['amount'] / 100);
            $row['limit_amount'] = money_format('%i', $row['limit_amount'] / 100);
        }

        return [
            'total' => $total,
            'data' => $res
        ];
    }

    /**
     * 获取一条记录
     * @param $id
     * @return mixed
     */
    public function get($id)
    {
        $db = clone($this->db);
        $query = $db->get_where('promoter_ticket', ['id' => $id]);
        return $query->row_array();
    }

    /**
     * 修改
     * @param $params
     * @return bool
     */
    public function edit($params)
    {
        $id = value($params, 'id', 0);
        if (empty($id)) {
            return false;
        }

        $params = $this->formatParams($params);
        $db = clone($this->db);
        $db->where('id', $id);
        $db->update('promoter_ticket', $params);
        if ($db->affected_rows()) {
            return true;
        }
        return false;
    }

    /**
     * 修改状态
     * @param $id
     * @param $status
     * @return bool
     */
    public function changStatus($id, $status)
    {
        $db = clone($this->db);
        $db->where('id', $id);
        $db->update('promoter_ticket', ['status' => $status]);
        if ($db->affected_rows()) {
            return true;
        }
        return false;
    }

    /**
     * 格式化参数
     * @param $params
     * @return array
     */
    public function formatParams($params)
    {
        return array(
            'name' => value($params, 'name', ''),
            'wechat' => !empty($params['wechat']) ? 1 : 0,
            'app' => !empty($params['app']) ? 1 : 0,
            'description' => value($params, 'description', ''),
            'num' => value($params, 'num', 0),
            'amount' => value($params, 'amount', 0),
            'limit_amount' => value($params, 'limitAmount', 0),
            'time_start' => !empty($params['timestart']) ? $params['timestart'] : '0000-00-00 00:00:00',
            'time_end' => !empty($params['timeend']) ? $params['timeend'] : '0000-00-00 00:00:00',
            'createdby' => !empty($params['createdby']) ? $params['createdby'] : adminId(),
            'updatedby' => adminId(),
            'status' => isset($params['status']) ? $params['status'] : 6
        );
    }
}